﻿// JavaScript Document
var mem = {};
var comet = {
	connection:false,
	iframediv:false,
	user:null,
	
	init:function(user){
		//comet.user = user;
		if (navigator.appVersion.indexOf("MSIE") != -1){
			comet.connection = new ActiveXObject("htmlfile");
			comet.connection.open();
			comet.connection.write('<html>');
			comet.connection.write("<script type='text/javascript'>document.domain = '"+document.domain+"'");
			comet.connection.write('</html>');
			comet.connection.close();
			comet.iframediv = comet.connection.createElement("div");
			comet.connection.appendChild(comet.iframediv);
			
			comet.connection.parentWindow.comet = comet;
			comet.iframediv.innerHTML = "<iframe id='comet_iframe' src='cometSev.php?user="+user+"&r="+(new Date().getTime())+"'></iframe>";
			//alert(comet.connection.parentWindow.comet);
		}else if(navigator.appVersion.indexOf("KHTML") != -1){
			comet.connection = document.createElement('iframe');
			comet.connection.setAttribute('id','comet_iframe');
			comet.connection.setAttribute('src','cometSev.php?user='+user+'&r='+(new Date().getTime()));
			comet.connection.style.display = 'none';
			document.body.appendChild(comet.connection);
		}else{
			comet.connection = document.createElement('iframe');
			comet.connection.setAttribute('id','comet_iframe');
			comet.connection.style.display = 'none';
			comet.connection.setAttribute('src','cometSev.php?user='+user+'&r='+(new Date().getTime()));
			document.body.appendChild(comet.connection);
		}
	},
	onUnload:function(){
		if(comet.connection){
			comet.connection.src = '';
		}
	},
	display:function(json){
		var from = json.from;
		var to = json.to;
		var msg = json.msg;
		var li = $('#sider li[name='+to+']')
		var box = $('#'+to).length == 0 ? $('#'+from) : $('#'+to);
		if(box.length == 0){
			var dataArr = [];
			li.find('em').html('-新消息');
			dataArr.push(json);
			mem[from] = dataArr;
		}else{
			box.find('.winContent').append($('<p>'+from+': '+msg+'</p>'));
			box.find('.winContent')[0].scrollTop = box.find('.winContent')[0].scrollHeight;
		}
		
	}
};

function sendMsg(from, to, msg){
	$.ajax({
		type:'POST',
		url:'receive.php',
		cache:false,
		data:'from='+from+'&msg='+msg+'&to='+to,
		success:function(resp){
			$('#'+to+' .msgArea').val('');
		}
	});
}